package com.aeye.mbr.servlet;

import com.aeye.mbr.common.base.BaseController;
import com.aeye.mbr.ws.util.RedisImgUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 日志controller
 * Created by shuaeye.mbr on 2017/3/14.
 */
@Controller
@Api(value = "照片上传", description = "照片上传")
@RequestMapping("/pic")
public class PicController extends BaseController {

    private static Logger _log = LoggerFactory.getLogger(PicController.class);

    @RequestMapping("/upload")
    public @ResponseBody  JSONObject upload(MultipartFile[] files){
        JSONObject result = new JSONObject();
        if(null == files || files.length < 1)
        {
            result.put("result",-1);
            result.put("message","no pic");
        }else{
            List<String> ids = new ArrayList<String>(files.length);
            for(MultipartFile f:files){
                try {
                    String id = RedisImgUtils.putImage(f.getBytes());
                    ids.add(id);
                } catch (Exception e) {
                    e.printStackTrace();
                    ids.add("");
                }
            }
            result.put("result",0);
            result.put("picIds",ids);
        }
        return result;
    }
}